<template>
  <div class="p-2">
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-card class="box-card">
          <template v-slot:header>
            <div class="clearfix">
              <span>个人信息</span>
            </div>
          </template>
          <div>
            <div class="text-center">
              <userAvatar/>
            </div>
            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <svg-icon icon-class="user" />用户名称
                <div class="pull-right">{{ state.user.userName }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="phone" />手机号码
                <div class="pull-right">{{ state.user.phonenumber }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="email" />用户邮箱
                <div class="pull-right">{{ state.user.email }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="tree" />所属部门
                <div class="pull-right" v-if="state.user.dept">{{ state.user.dept.deptName }} / {{ state.postGroup }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="peoples" />所属角色
                <div class="pull-right">{{ state.roleGroup }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="date" />创建日期
                <div class="pull-right">{{ state.user.createTime }}</div>
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card>
          <template v-slot:header>
            <div class="clearfix">
              <span>基本资料</span>
            </div>
          </template>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="基本资料" name="userinfo">
              <userInfo :user="userForm" />
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="resetPwd">
              <resetPwd />
            </el-tab-pane>
            <el-tab-pane label="第三方应用" name="thirdParty">
              <thirdParty :auths="state.auths" />
            </el-tab-pane>
            <el-tab-pane label="聊天" name="chat">
              <div class="chat-container">
                <div class="chat-header">
                  <h3>即时通讯</h3>
                  <p>与同事进行实时沟通交流</p>
                </div>
                <div class="chat-content">
                  <div class="chat-placeholder">
                    <el-icon size="64" color="#409EFF">
                      <ChatDotRound />
                    </el-icon>
                    <h4>开始聊天</h4>
                    <p>点击下方按钮打开聊天界面，与同事进行实时沟通</p>
                    <el-button type="primary" size="large" @click="openChatDialog">
                      <el-icon><ChatDotRound /></el-icon>
                      打开聊天
                    </el-button>
                  </div>
                </div>
              </div>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
    
    <!-- 聊天弹窗 -->
    <ChatDialog v-model="showChatDialog" />
  </div>
</template>

<script setup name="Profile" lang="ts">
import UserAvatar from "./userAvatar.vue";
import UserInfo from "./userInfo.vue";
import ResetPwd from "./resetPwd.vue";
import ThirdParty from "./thirdParty.vue";
import ChatDialog from "@/components/ChatDialog/index.vue";
import { getAuthList } from "@/api/system/social/auth";
import { getUserProfile } from "@/api/system/user";
import useImStore from '@/store/modules/im';
import { ElMessage } from 'element-plus';

const activeTab = ref("userinfo");
const state = ref<Record<string, any>>({
    user: {},
    roleGroup: '',
    postGroup: '',
    auths: []
});

const userForm = ref({});
const imStore = useImStore();

// 聊天弹窗状态
const showChatDialog = ref(false);

// 打开聊天弹窗
const openChatDialog = async () => {
  try {
    console.log('打开聊天弹窗...')
    
    // 直接打开聊天弹窗，不进行IM登录检查
    showChatDialog.value = true;
    
    // 异步进行IM登录（不阻塞弹窗打开）
    if (!imStore.isConnected) {
      imStore.loginToIm().then(success => {
        if (success) {
          console.log('IM登录成功')
        } else {
          console.log('IM登录失败，但不影响聊天弹窗使用')
        }
      }).catch(error => {
        console.error('IM登录异常:', error)
      })
    }
  } catch (error) {
    console.error('打开聊天失败:', error);
    ElMessage.error('打开聊天失败');
  }
}

const getUser = async () => {
    const res = await getUserProfile();
    state.value.user = res.data.user;
    userForm.value = { ...res.data.user }
    state.value.roleGroup = res.data.roleGroup;
    state.value.postGroup = res.data.postGroup;
};

const getAuths = async () => {
    const res = await getAuthList();
    state.value.auths = res.data;
};

onMounted(() => {
    getUser();
    getAuths();
})
</script>

<style scoped>
.chat-container {
  padding: 20px;
}

.chat-header {
  text-align: center;
  margin-bottom: 30px;
}

.chat-header h3 {
  margin: 0 0 10px 0;
  color: #303133;
  font-size: 24px;
}

.chat-header p {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.chat-content {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}

.chat-placeholder {
  text-align: center;
  padding: 40px;
}

.chat-placeholder h4 {
  margin: 20px 0 10px 0;
  color: #303133;
  font-size: 18px;
}

.chat-placeholder p {
  margin: 0 0 30px 0;
  color: #909399;
  font-size: 14px;
  line-height: 1.5;
}
</style>
